package tech.summerly.weather.component.adapter.floors

import android.view.LayoutInflater
import android.view.ViewGroup
import com.github.tianma8023.formatter.SunriseSunsetLabelFormatter
import com.github.tianma8023.model.Time
import com.google.gson.JsonArray
import kotlinx.android.synthetic.main.floor_sun_rise_set.view.*
import tech.summerly.weather.R
import tech.summerly.weather.data.bean.RealTime
import tech.summerly.weather.util.ViewHolder
import tech.summerly.weather.util.log

class SunRiseSetFloor : BaseFloor<SunRiseSet>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return createHolder(R.layout.floor_sun_rise_set, inflater, parent)
    }


    override fun onBindViewHolder(holder: ViewHolder, item: SunRiseSet) = with(holder.itemView) {
        sunriseView.sunriseTime = item.sunriseTime
        sunriseView.sunsetTime = item.sunsetTime
        sunriseView.startAnimate()
        sunriseView.labelFormatter = object : SunriseSunsetLabelFormatter {
            override fun formatSunsetLabel(sunset: Time): String {
                return "日落 %d:%d".format(sunset.hour, sunset.minute)
            }

            override fun formatSunriseLabel(sunrise: Time): String {
                return "日出 %d:%d".format(sunrise.hour, sunrise.minute)
            }
        }
        Unit
    }

}


class SunRiseSet(val realTime: RealTime, astro: JsonArray) {

    val sunriseTime: Time
    val sunsetTime: Time

    init {
        val sunrise = astro[0].asJsonObject["sunrise"].asJsonObject["time"].asString.split(':')
        val sunset = astro[0].asJsonObject["sunset"].asJsonObject["time"].asString.split(':')
        log { "sunrise : $sunrise , sunset:$sunset" }
        sunriseTime = Time(sunrise[0].toInt(), sunrise[1].toInt())
        sunsetTime = Time(sunset[0].toInt(), sunset[1].toInt())
    }


}